<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>websocket通讯client1</title>
<!--    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>-->
    <script src="libs/jquery.min.js"></script>
    <script src="libs/bootstrap.min.js"></script>
    <script src="libs/bootstrap-table.min.js"></script>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/bootstrap-table.min.css">
    <link rel="stylesheet" href="css/jquery-ui.min.css">
</head>
<script>
    var socket;
    function openSocket() {
        if(typeof(WebSocket) == "undefined") {
            console.log("您的浏览器不支持WebSocket");
        }else{
            console.log("您的浏览器支持WebSocket");
            //实现化WebSocket对象，指定要连接的服务器地址与端口  建立连接
            var socketUrl="http://localhost:8080/imserver/"+$("#userId").val();
            socketUrl=socketUrl.replace("https","ws").replace("http","ws");
            console.log(socketUrl);
            if(socket!=null){
                socket.close();
                socket=null;
            }
            socket = new WebSocket(socketUrl);
            //打开事件
            socket.onopen = function() {
                console.log("websocket已打开");
                //socket.send("这是来自客户端的消息" + location.href + new Date());
            };
            //获得消息事件
            socket.onmessage = function(msg) {
                console.log("接收到服务端消息：" + msg.data);
                var data = JSON.parse(msg.data);
                if(data.code=="200") {
                    console.log(data.msg);
                    var msgLine = $("#reciveMsg").html();
                    msgLine += "<div>"+data.msg+"</div>";
                    $("#reciveMsg").html(msgLine);
                    if(data.onlineCount) {
                        $("#onlineCount").html(data.onlineCount);
                    }
                } else {
                    console.log("连接失败");
                }

                //发现消息进入    开始处理前端触发逻辑
            };
            //关闭事件
            socket.onclose = function() {
                console.log("websocket已关闭");
            };
            //发生了错误事件
            socket.onerror = function() {
                console.log("websocket发生了错误");
            }
        }
    }
    function sendMessage() {
        if(typeof(WebSocket) == "undefined") {
            console.log("您的浏览器不支持WebSocket");
        }else {
            console.log("您的浏览器支持WebSocket");
            var toUserId = $("#toUserId").val();
            var contentText = $("#contentText").val();
            console.log('{"toUserId":"'+toUserId+'","contentText":"'+contentText+'"}');
            socket.send('{"toUserId":"'+toUserId+'","contentText":"'+contentText+'"}');
        }
    }
    function closeSocket() {
        socket.close();
        socket=null;
        var msgLine = $("#reciveMsg").html();
        msgLine += "<div>连接关闭</div>";
        $("#reciveMsg").html(msgLine);
    }
</script>
<body>
<table class="table table-bordered">
    <tbody>
        <tr>
            <td class="text-right col-md-3">【当前在线人数】：</td>
            <td><span id="onlineCount">0</span> 人</td>
        </tr>
        <tr>
            <td class="text-right col-md-3">【fromUserId】：</td>
            <td><input id="userId" name="userId" type="text" value="101" class="form-control" style="width: 30%;"></td>
        </tr>
        <tr>
            <td class="text-right">【toUserId】：</td>
            <td><input id="toUserId" name="toUserId" type="text" value="102" class="form-control" style="width: 30%;"></td>
        </tr>
        <tr>
            <td class="text-right">【contentText】：</td>
            <td><input id="contentText" name="contentText" type="text" value="Hello World!" class="form-control"></td>
        </tr>
        <tr>
            <td class="text-right">【操作】：</td>
            <td>
                <input type="button" onclick="openSocket()" th:value="开启socket" class="btn btn-primary"/>
                <input type="button" onclick="closeSocket()" th:value="关闭socket" class="btn btn-primary" style="margin-left: 30px;"/>
            </td>
        </tr>
        <tr>
            <td class="text-right">【操作】：</td>
            <td><input type="button" onclick="sendMessage()" th:value="发送消息" class="btn btn-primary"/></td>
        </tr>
        <tr>
            <td class="text-right">【接收消息】：</td>
            <td><div id="reciveMsg"></div></td>
        </tr>
    </tbody>
</table>
</body>

</html>